package com.wyl.salary.ui.viewmodel

import android.app.Application
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.content.edit
import androidx.lifecycle.AndroidViewModel
import java.lang.NumberFormatException
import java.text.ParseException

class DefaultSalaryViewModel(app: Application) : AndroidViewModel(app) {
    private val sp = app.getSharedPreferences("salary", 0)

    var defaultSalary by mutableStateOf(sp.getString("defaultSalary", "")!!)

    fun save(): Boolean {
        return try {
            defaultSalary.toDouble()
            sp.edit {
                putString("defaultSalary", defaultSalary)
            }
            true
        } catch (e: NumberFormatException) {
            e.printStackTrace()
            false
        }
    }

    fun onDefaultSalaryChanged(newValue: String) {
        defaultSalary = newValue
    }
}